/**
 * 用户信息角色配置页面JS
 * User: zhaoming
 * Date: 15-8-17
 * Time: 下午5:58
 * To change this template use File | Settings | File Templates.
 */
$(function(){
    //激活左侧导航菜单
    activeNavBar("MANAGE_USER_INFO");
    loadGrid();
    loadRoleGrid();

    $("#headGoBackBtn").click(function(){
        window.forward(springUrl+ "/web/userInfo/index");
    });
    $("#addBtn").click(function(){
        openDialog();
    });
    $("#queryBtn").click(function(){
        queryRoleInfo();
    });
    $("#resetBtn").click(function(){
        $('#searchForm')[0].reset();
        $('#searchForm').find('.select2-chosen').html('请选择');
    });
});

/**
 * 加载数据列表
 */
function loadGrid() {
    var gridTable = jQuery("#gridTable");
    gridTable.jqGrid({
        mtype: "POST",
        url: springUrl + "/web/userInfo/roleUserRelationPage",
        postData: {
            userNo: $("#uniqueNo").val()
        },
        datatype: "json",
        autowidth: true,
        height: "100%",
        colModel: [
            {name: 'uniqueNo', label: 'uniqueNo', width: 50, sortable: false, resizable: false, hidden: true},
            {name: 'roleInfoVo.roleCode', label: '角色编码', width: 100, sortable: false, align: 'left'},
            {name: 'roleInfoVo.roleName', label: '角色名称', width: 100, sortable: false, align: 'left'},
            {name: 'roleInfoVo.roleTypeName', label: '角色类型', width: 100, sortable: false, align: 'left'},
            {name: '', label: '操作', index: '', width: 140, fixed: true, sortable: false, resize: false, align: 'center',
                formatter: function (cellValue, options, rowObject) {
                    var context = "";
                    context += '<button class="mfui-btn mfui-btn-primary mfui-btn-sm" onclick="deleteByUniqueNo(\''+rowObject.uniqueNo+'\')" title="删除">删除</button>';
                    return context;
                }
            }
        ],
        rownumbers: true,//添加左侧行号
        viewrecords: true,
        rowNum: 10,
        rowList: [10, 20, 30],
        sortname: "create_time",
        sortorder: "desc",
        altRows: true,
        pager: "#gridPager",
        jsonReader: { //server返回Json解析设定
            root: "content", //对于json中数据列表
            page: "pageNum",
            total: "total",
            records: "records"
        }
    });
    //navButtons
    gridTable.jqGrid('navGrid', "#gridPager",
        {
            add: false,
            addicon: 'fa-plus purple',
            edit: false,
            editicon: 'fa-pencil blue',
            del: false,
            delicon: 'fa-trash red',
            view: false,
            viewicon: 'fa-search-plus orange',
            search: false,
            searchicon: 'fa-search grey',
            refresh: true,
            refreshicon: 'fa-refresh green'
        }
    );
    updatePagerIcons(this);
    resizeJqGrid("gridTable");
}

/**
 * 根据主键删除
 */
function deleteByUniqueNo(uniqueNo) {
    Alert.confirm("您确定删除该条信息？", function () {
        jQuery.ajax({
            type:"POST",
            dataType:'json',
            url: springUrl+"/web/userInfo/deleteRoleUserRelation",
            data:{
                uniqueNo:  uniqueNo
            },
            success:function (data) {
                Alert.success("执行成功！", function () {
                    $("#gridTable").trigger("reloadGrid");
                });
            },
            error : function(data){
                Alert.error(data.responseText);
            }
        });
    });
}

/**
 * 打开模态框
 */
function openDialog(){
    layer.open({
        id: "role-dialog",
        title: '角色信息',
        zIndex: 5000,
        type: 1,
        skin: 'dialog-blue-class',
        area: ['800px', '500px'],
        content: $("#role-content"),
        btn: ['添加'],
        yes: function(index, layero){
            saveRelation(index);
        },
        cancel: function(index, layero){
            //右上角关闭回调
            $("#role-content").hide();
        }
    });
    //永久显示弹出层纵向滚动条
    $("#role-dialog").css({"overflow-y": "scroll" });
    //查询角色信息
    queryRoleInfo();
}

/**
 * 加载数据列表
 */
function loadRoleGrid(){
    var gridTable = jQuery("#roleGridTable");
    gridTable.jqGrid({
        mtype: "POST",
        datatype: "local",
        width: "757",
        height: "100%",
        colModel: [
            {name:'uniqueNo', label:'uniqueNo', index:'', width:50, sortable: false, resizable:false, hidden: true},
            {name:'roleCode',label:'角色编码', index:'',width:100, sortable: false, align:'left'},
            {name:'roleName',label:'角色名称', index:'', width:100, sortable: false, align:'left'}
        ],
        rownumbers: true,//添加左侧行号
        viewrecords : true,
        rowNum: 10,
        rowList: [10,20,30],
        sortname: "create_time",
        sortorder: "desc",
        altRows: true,
        pager : "#roleGridPager",
        jsonReader: { //server返回Json解析设定
            root: "content", //对于json中数据列表
            page: "pageNum",
            total: "total",
            records: "records"
        }
    });
    //navButtons
    gridTable.jqGrid('navGrid',"#roleGridPager",
        {
            add: false,
            addicon : 'fa-plus purple',
            edit: false,
            editicon : 'fa-pencil blue',
            del: false,
            delicon : 'fa-trash red',
            view: false,
            viewicon : 'fa-search-plus orange',
            search: false,
            searchicon : 'fa-search grey',
            refresh: true,
            refreshicon : 'fa-refresh green'
        }
    );
    updatePagerIcons(this);
}

/**
 * 查询
 */
function queryRoleInfo(){
    if($('#searchForm').trigger("validate").isValid()){
        var gridParam = {
            datatype: "json",
            url: springUrl+"/web/userInfo/rolePage",
            postData:{
                roleCode: $("#roleCode").val(),
                roleName: $("#roleName").val()
            },
            page:1
        };
        $("#roleGridTable").jqGrid("setGridParam", gridParam).trigger("reloadGrid");
    }
}


/**
 * 保存关联信息
 * @param index
 */
function saveRelation(index){
    var rowId = $("#roleGridTable").jqGrid("getGridParam","selrow");
    if(rowId == null || rowId == ""){
        Alert.warning("至少选择一个角色绑定");
        return false;
    }
    var rowData = $("#roleGridTable").jqGrid("getRowData",rowId);
    jQuery.ajax({
        type:"POST",
        dataType:'json',
        url: springUrl+"/web/userInfo/insertRoleUserRelation",
        data:{
            userNo: $("#uniqueNo").val(),
            roleNo: rowData.uniqueNo
        },
        success:function (data) {
            Alert.success("执行成功！", function () {
                layer.close(index);
                $("#role-content").hide();
                $("#gridTable").trigger("reloadGrid");
            });
        },
        error : function(data){
            Alert.error(data.responseText);
        }
    });
}
